package com.platform.module.admin.controller;

import com.platform.module.admin.service.UserService;
import com.platform.module.common.base.BaseReq;
import com.platform.module.common.base.CodeEnum;
import com.platform.module.common.base.CommonResp;
import com.platform.module.common.base.NoNeedLogin;
import com.platform.module.common.exception.CommonException;
import com.platform.module.common.utils.MD5Helper;
import com.platform.module.pojo.admin.dto.AdminLoginDto;
import com.platform.module.pojo.admin.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  管理后台用户前端控制器
 * </p>
 *
 * @author pxj
 * @since 2025-02-12
 */

@Slf4j
@RestController
@RequestMapping("/module/tbAdminUser")
@AllArgsConstructor
@Tag(name = "管理端")
public class AdminUserController {

    private final UserService userService;

    @Operation(summary = "登录")
    @NoNeedLogin
    @PostMapping("/login.api")
    public CommonResp<UserInfoVo> login(@Parameter(hidden = true) BaseReq req,
                                        @RequestBody AdminLoginDto adminLoginDto) {
        String sign = adminLoginDto.getSign();
        String md5Sign = MD5Helper.md5NoKey(adminLoginDto.getUserId()+adminLoginDto.getPassword());
        if (!sign.equals(md5Sign)) {
            throw new CommonException(CodeEnum.参数值错误);
        }
        UserInfoVo login = userService.login(adminLoginDto.getUserId(), adminLoginDto.getPassword());
        return CommonResp.ok(login);
    }

    @Operation(summary = "登出")
    @GetMapping("/logout.api")
    public CommonResp<UserInfoVo> logout(@Parameter(hidden = true) BaseReq req) {
        userService.logout(req.getAccessToken());
        return CommonResp.ok();
    }

    public static void main(String[] args) {
        String passwordMd5 = MD5Helper.md5NoKey("123456");
        System.out.println("password = "+ passwordMd5);
        String sign = MD5Helper.md5NoKey("1"+passwordMd5);
        System.out.println("sign = "+ sign);
    }

}
